<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>放大镜</title>
    <style>
        .goods {
            position: relative;
            width: 400px;
            height: 400px;
            margin: 200px auto;
            border: 1px solid #ccc;
        }
        
        .goods img {
            width: 100%;
            height: 100%;
        }
        
        .mask {
            display: none;
            position: absolute;
            top: 0;
            left: 0;
            width: 230px;
            height: 230px;
            background-color: yellow;
            opacity: .3;
            cursor: move;
        }
        
        .large {
            display: none;
            position: absolute;
            top: 136px;
            right: 248px;
            width: 500px;
            height: 500px;
            overflow: hidden;
            border: 1px solid #ccc;
        }
        
        .large img {
            position: absolute;
            top: 0;
            left: 0;
        }
    </style>
    <script>
        window.addEventListener('DOMContentLoaded', function() {
            var goods = document.querySelector('.goods');
            var mask = document.querySelector('.mask');
            var large = document.querySelector('.large');
            goods.addEventListener('mouseenter', function() {
                mask.style.display = 'block';
                large.style.display = 'block';
            });
            goods.addEventListener('mouseleave', function() {
                mask.style.display = 'none';
                large.style.display = 'none';
            });
            goods.addEventListener('mousemove', function(e) {
                var distanceX = e.pageX - this.offsetLeft;
                var distanceY = e.pageY - this.offsetTop;
                var maskX = distanceX - mask.offsetWidth / 2;
                var maskY = distanceY - mask.offsetHeight / 2;
                var maskMax = goods.offsetWidth - mask.offsetWidth;
                if (maskX <= 0) {
                    maskX = 0;
                } else if (maskX >= maskMax) {
                    maskX = maskMax;
                }
                if (maskY <= 0) {
                    maskY = 0;
                } else if (maskY >= maskMax) {
                    maskY = maskMax;
                }
                mask.style.left = maskX + 'px';
                mask.style.top = maskY + 'px';
                var largeMax = large.children[0].offsetWidth - large.offsetWidth;
                var largeX = maskX * largeMax / maskMax;
                var largeY = maskY * largeMax / maskMax;
                large.children[0].style.top = -largeY + 'px';
                large.children[0].style.left = -largeX + 'px';
            });
        });
    </script>
</head>

<body>
    <div class="goods">
        <img src="images/s3.png" alt="">
        <div class="mask"></div>
    </div>
    <div class="large">
        <img src="images/big.jpg" alt="">
    </div>
</body>

</html>